package com.jie.jie.mvp.ui;

import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.jaydenxiao.common.base.BaseActivity;
import com.jaydenxiao.common.v.recyclerview.RecycleViewDivider;
import com.jaydenxiao.common.v.recyclerview.SwipeRecyclerView;
import com.jie.jie.R;
import com.jie.jie.mvp.adpter.MvpGanhuoRvAdapter;
import com.jie.jie.mvp.adpter.MvpMeiziRvAdapter;
import com.jie.jie.mvp.adpter.MvpVpAdapter;
import com.jie.jie.mvp.contract.MvpContract;
import com.jie.jie.mvp.model.MvpdataBean;
import com.jie.jie.mvp.model.TestModel;
import com.jie.jie.mvp.persenter.MvpPresenter;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.OnClick;

public class MVPActivity extends BaseActivity<MvpPresenter, TestModel> implements MvpContract.View {


    @Bind(R.id.back_iv)
    ImageView backIv;
    @Bind(R.id.title_tv)
    TextView titleTv;
    @Bind(R.id.mvp_tab)
    TabLayout mvpTab;
    @Bind(R.id.mvp_vp)
    ViewPager mvpVp;
    @Bind(R.id.activity_mvp)
    LinearLayout activityMvp;
    private List<String> titles;
    private List<View> views;
    private Context context;
    private List<MvpdataBean.ResultsBean> data1, data2;
    private View view;
    private View view1;
    private MvpMeiziRvAdapter meiziRvAdapter;
    private MvpGanhuoRvAdapter ganhuoRvAdapter;
    private MvpVpAdapter mvpVpAdapter;
    private SwipeRecyclerView recyclerView1, recyclerView2;
    private int pagesize1, pagesize2;


    @Override
    public int getLayoutId() {
        return R.layout.activity_mvp;
    }

    @Override
    public void initPresenter() {
        mPresenter.setVM(this, mModel);
    }

    @RequiresApi(api = Build.VERSION_CODES.KITKAT)
    @Override
    public void initView() {
        context = this;
        data1 = new ArrayList<>();
        data2 = new ArrayList<>();
        titles = new ArrayList<>();
        titles.add("干货");
        titles.add("妹子");
        views = new ArrayList<>();
        view = LayoutInflater.from(context).inflate(R.layout.mvp_view, null);
        views.add(view);
        view1 = LayoutInflater.from(context).inflate(R.layout.mvp_view, null);
        views.add(view1);
        mvpVpAdapter = new MvpVpAdapter(views, titles);
        mvpVp.setAdapter(mvpVpAdapter);
        mvpTab.setupWithViewPager(mvpVp);
        initItemView();
//        Transition explode = TransitionInflater.from(this).inflateTransition(R.transition.explode);
    }

    private void initItemView() {

        pagesize1 = 100;
        pagesize2 = 10;
        mPresenter.getTextRequest(pagesize1, 1);
        mPresenter.getText1Request(pagesize2, 1);
        recyclerView2 = (SwipeRecyclerView) view1.findViewById(R.id.mvp_view_rv);
        recyclerView1 = (SwipeRecyclerView) view.findViewById(R.id.mvp_view_rv);
        meiziRvAdapter = new MvpMeiziRvAdapter(context, data2);
        ganhuoRvAdapter = new MvpGanhuoRvAdapter(data1, context);

//        final StaggeredGridLayoutManager layoutManager = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);
//        layoutManager.setGapStrategy(StaggeredGridLayoutManager.GAP_HANDLING_NONE);
        recyclerView2.getRecyclerView().setLayoutManager(new GridLayoutManager(context,2));
//        recyclerView2.getRecyclerView().addOnScrollListener(new RecyclerView.OnScrollListener() {
//            @Override
//            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
//                super.onScrollStateChanged(recyclerView, newState);
//                layoutManager.invalidateSpanAssignments(); //防止第一行到顶部有空白区域
//            }
//        });

        recyclerView2.setAdapter(meiziRvAdapter);
        recyclerView2.getRecyclerView().addItemDecoration(new RecycleViewDivider());
        recyclerView2.setOnLoadListener(new SwipeRecyclerView.OnLoadListener() {
            @Override
            public void onRefresh() {
                mPresenter.getTextRequest(pagesize1, 1);
            }

            @Override
            public void onLoadMore() {
                pagesize1 += 100;
                mPresenter.getTextRequest(pagesize1, 1);
            }
        });

        meiziRvAdapter.setItemClick(new MvpMeiziRvAdapter.ItemClick() {
            @Override
            public void click(View view, String url,String title) {
                Intent intent = new Intent(context, MeiziActivity.class);
                intent.putExtra("url", url);
                intent.putExtra("title", title);
                startActivity(intent);
            }
        });


        recyclerView1.getRecyclerView().setLayoutManager(new LinearLayoutManager(context));
        recyclerView1.setAdapter(ganhuoRvAdapter);
        recyclerView1.getRecyclerView().addItemDecoration(new RecycleViewDivider());

        recyclerView1.setOnLoadListener(new SwipeRecyclerView.OnLoadListener() {
            @Override
            public void onRefresh() {
                mPresenter.getText1Request(pagesize2, 1);
            }

            @Override
            public void onLoadMore() {
                pagesize2 += 10;
                mPresenter.getText1Request(pagesize2, 1);
            }
        });
        ganhuoRvAdapter.setItemClick(new MvpGanhuoRvAdapter.ItemClick() {
            @Override
            public void click(View view, String url) {
                Intent intent = new Intent(context, GanhuoActivity.class);
                intent.putExtra("url", url);
                startActivity(intent);
            }
        });

    }


    @Override
    public void showLoading(String title) {

    }

    @Override
    public void stopLoading() {

    }

    @Override
    public void showErrorTip(String msg) {

    }


    @OnClick(R.id.back_iv)
    public void onClick() {
        MVPActivity.this.finish();
    }

    @Override
    public void returnfuli(MvpdataBean mvpdataBean) {

            data2.addAll(mvpdataBean.getResults());
        meiziRvAdapter.notifyItemChanged(pagesize2-11);
        recyclerView2.stopLoadingMore();
        recyclerView2.complete();


    }

    @Override
    public void returnAndroid(MvpdataBean mvpdataBean) {
        recyclerView1.stopLoadingMore();
        recyclerView1.complete();
        data1.clear();
        data1.addAll(mvpdataBean.getResults());
        ganhuoRvAdapter.notifyDataSetChanged();

    }
}
